﻿

Option Explicit On
'*******************************************************************
' Program: pkgVisio
' Author: Albert E Edlund
' Date:
'
' Purpose:
' Working with office object XML
'
'*******************************************************************

#Region " Imports "

Imports System
Imports System.Text
Imports System.Text.StringBuilder
Imports System.IO

Imports System.Xml

Imports System.Reflection
Imports System.Runtime.InteropServices

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging


#End Region


Public Class clsStructures


    '
    '
    '
    Public Enum pkgpartTypeEnum
        Package
        Part
        ExternalRelationship
    End Enum

    '
    '
    '
    Public Structure pkgpartInfo

        Public pkgpartPath As String
        Public pkgpartType As pkgpartTypeEnum
        Public pkgpartParent As String
        Public pkgpartRelationship As String
        Public pkgpartContentType As String
        Public pkgpartResourceId As String

        Public Sub New _
            (ByVal pkgpartPath As String, _
             ByVal pkgpartType As pkgpartTypeEnum, _
             ByVal pkgpartParent As String, _
             ByVal pkgpartRelationship As String, _
             ByVal pkgpartContentType As String, _
             ByVal pkgpartResourceId As String)

            Me.pkgpartPath = pkgpartPath
            Me.pkgpartType = pkgpartType
            Me.pkgpartParent = pkgpartParent
            Me.pkgpartRelationship = pkgpartRelationship
            Me.pkgpartContentType = pkgpartContentType
            Me.pkgpartResourceId = pkgpartResourceId
        End Sub

    End Structure

    '
    '
    '
    Public Structure stcPageShape
        Dim psPage As Long
        Dim psShape As Long
    End Structure

    '
    '
    '
    Public Structure stcShapeInfo

        Public lngPgId As Long
        Public lngShpId As Long
        Public strShpName As String
        Public strShpNameU As String
        Public strShpType As String
        Public lngShpParent As Long

        Sub New _
            (ByVal shpPageId As Long, _
             ByVal shpShapeId As Long, _
             ByVal shpName As String, _
             ByVal shpNameU As String, _
             ByVal shpType As String, _
             ByVal shpParent As Long)


            Me.lngPgId = shpPageId
            Me.lngShpId = shpShapeId
            Me.strShpName = shpName
            Me.strShpNameU = shpNameU
            Me.strShpType = shpType
            Me.lngShpParent = shpParent

        End Sub

    End Structure


#Region " Cells "

    ' line 153 in v15Schema
    '    <!--Cell base type-->
    '<xsd:complexType name="Cell_Type" mixed="true">
    '  <xsd:sequence>
    '    <xsd:element name="RefBy" type="RefBy_Type" minOccurs="0"
    '                 maxOccurs="unbounded" />
    '  </xsd:sequence>
    '  <xsd:attribute name="N" type="xsd:string" use="required" />
    '  <xsd:attribute name="U" type="xsd:string" />
    '  <xsd:attribute name="E" type="xsd:string" />
    '  <xsd:attribute name="F" type="xsd:string" />
    '  <xsd:attribute name="V" type="xsd:string" />
    '</xsd:complexType>

    '
    ' section 2.4.4
    '
    Private Function buildCellTypes() As Dictionary(Of String, Integer)

        Dim dictCells As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)

        dictCells.Add("a", 1)
        dictCells.Add("action", 2)
        dictCells.Add("active", 3)
        dictCells.Add("addmarkup", 4)
        dictCells.Add("address", 5)
        dictCells.Add("alignbottom", 6)
        dictCells.Add("aligncenter", 7)
        dictCells.Add("alignleft", 8)
        dictCells.Add("alignment", 9)
        dictCells.Add("alignmiddle", 10)
        dictCells.Add("alignright", 11)
        dictCells.Add("aligntop", 12)
        dictCells.Add("angle", 13)
        dictCells.Add("asianfont", 14)
        dictCells.Add("autogen", 15)
        dictCells.Add("avenuesizex", 16)
        dictCells.Add("avenuesizey", 17)
        dictCells.Add("avoidpagebreak", 18)
        dictCells.Add("b", 19)
        dictCells.Add("beginarrow", 20)
        dictCells.Add("beginarrowsize", 21)
        dictCells.Add("begingroup", 22)
        dictCells.Add("beginx", 23)
        dictCells.Add("beginy", 24)
        dictCells.Add("begtrigger", 25)
        dictCells.Add("bevelbottomheight", 26)
        dictCells.Add("bevelbottomtype", 27)
        dictCells.Add("bevelbottomwidth", 28)
        dictCells.Add("bevelcontourcolor", 29)
        dictCells.Add("bevelcontoursize", 30)
        dictCells.Add("beveldepthcolor", 31)
        dictCells.Add("beveldepthsize", 32)
        dictCells.Add("bevellightingangle", 33)
        dictCells.Add("bevellightingtype", 34)
        dictCells.Add("bevelmaterialtype", 35)
        dictCells.Add("beveltopheight", 36)
        dictCells.Add("beveltoptype", 37)
        dictCells.Add("beveltopwidth", 38)
        dictCells.Add("blocksizex", 39)
        dictCells.Add("blocksizey", 40)
        dictCells.Add("blur", 41)
        dictCells.Add("bottommargin", 42)
        dictCells.Add("brightness", 43)
        dictCells.Add("bullet", 44)
        dictCells.Add("bulletfont", 45)
        dictCells.Add("bulletfontsize", 46)
        dictCells.Add("bulletstr", 47)
        dictCells.Add("buttonface", 48)
        dictCells.Add("c", 49)
        dictCells.Add("calendar", 50)
        dictCells.Add("canglue", 51)
        dictCells.Add("case", 52)
        dictCells.Add("categorychanged", 53)
        dictCells.Add("centerx", 54)
        dictCells.Add("centery", 55)
        dictCells.Add("checked", 56)
        dictCells.Add("clippingpath", 57)
        dictCells.Add("color", 58)
        dictCells.Add("colorschemeindex", 59)
        dictCells.Add("colortrans", 60)
        dictCells.Add("comment", 61)
        dictCells.Add("complexscriptfont", 62)
        dictCells.Add("complexscriptsize", 63)
        dictCells.Add("compoundtype", 64)
        dictCells.Add("confixedcode", 65)
        dictCells.Add("conlinejumpcode", 66)
        dictCells.Add("conlinejumpdirx", 67)
        dictCells.Add("conlinejumpdiry", 68)
        dictCells.Add("conlinejumpstyle", 69)
        dictCells.Add("conlinerouteext", 70)
        dictCells.Add("connectorschemeindex", 71)
        dictCells.Add("contrast", 72)
        dictCells.Add("copyright", 73)
        dictCells.Add("ctrlasinput", 74)
        dictCells.Add("currentindex", 75)
        dictCells.Add("d", 76)
        dictCells.Add("datalinked", 77)
        dictCells.Add("date", 78)
        dictCells.Add("dblunderline", 79)
        dictCells.Add("default", 80)
        dictCells.Add("defaulttabstop", 81)
        dictCells.Add("denoise", 82)
        dictCells.Add("description", 83)
        dictCells.Add("dirx", 84)
        dictCells.Add("diry", 85)
        dictCells.Add("disabled", 86)
        dictCells.Add("displaylevel", 87)
        dictCells.Add("displaymode", 88)
        dictCells.Add("distancefromground", 89)
        dictCells.Add("doclangid", 90)
        dictCells.Add("doclockreplace", 91)
        dictCells.Add("dontmovechildren", 92)
        dictCells.Add("doublestrikethrough", 93)
        dictCells.Add("drawingresizetype", 94)
        dictCells.Add("drawingscale", 95)
        dictCells.Add("drawingscaletype", 96)
        dictCells.Add("drawingsizetype", 97)
        dictCells.Add("droponpagescale", 98)
        dictCells.Add("dynamicsoff", 99)
        dictCells.Add("dynfeedback", 100)
        dictCells.Add("e", 101)
        dictCells.Add("effectschemeindex", 102)
        dictCells.Add("enablefillprops", 103)
        dictCells.Add("enablegrid", 104)
        dictCells.Add("enablelineprops", 105)
        dictCells.Add("enabletextprops", 106)
        dictCells.Add("endarrow", 107)
        dictCells.Add("endarrowsize", 108)
        dictCells.Add("endtrigger", 109)
        dictCells.Add("endx", 110)
        dictCells.Add("endy", 111)
        dictCells.Add("eventdblclick", 112)
        dictCells.Add("eventdrop", 113)
        dictCells.Add("eventmultidrop", 114)
        dictCells.Add("eventxfmod", 115)
        dictCells.Add("extrainfo", 116)
        dictCells.Add("fillbkgnd", 117)
        dictCells.Add("fillbkgndtrans", 118)
        dictCells.Add("fillforegnd", 119)
        dictCells.Add("fillforegndtrans", 120)
        dictCells.Add("fillgradientangle", 121)
        dictCells.Add("fillgradientdir", 122)
        dictCells.Add("fillgradientenabled", 123)
        dictCells.Add("fillpattern", 124)
        dictCells.Add("flags", 125)
        dictCells.Add("flipx", 126)
        dictCells.Add("flipy", 127)
        dictCells.Add("flyoutchild", 128)
        dictCells.Add("font", 129)
        dictCells.Add("fontscale", 130)
        dictCells.Add("fontschemeindex", 131)
        dictCells.Add("format", 132)
        dictCells.Add("frame", 133)
        dictCells.Add("gamma", 134)
        dictCells.Add("glowcolor", 135)
        dictCells.Add("glowcolortrans", 136)
        dictCells.Add("glowsize", 137)
        dictCells.Add("glue", 138)
        dictCells.Add("gluetype", 139)
        dictCells.Add("gradientstopcolor", 140)
        dictCells.Add("gradientstopcolortrans", 141)
        dictCells.Add("gradientstopposition", 142)
        dictCells.Add("height", 143)
        dictCells.Add("helptopic", 144)
        dictCells.Add("hideforapply", 145)
        dictCells.Add("hidetext", 146)
        dictCells.Add("horzalign", 147)
        dictCells.Add("imgheight", 148)
        dictCells.Add("imgoffsetx", 149)
        dictCells.Add("imgoffsety", 150)
        dictCells.Add("imgwidth", 151)
        dictCells.Add("indfirst", 152)
        dictCells.Add("indleft", 153)
        dictCells.Add("indright", 154)
        dictCells.Add("inhibitsnap", 155)
        dictCells.Add("initials", 156)
        dictCells.Add("invisible", 157)
        dictCells.Add("isdropsource", 158)
        dictCells.Add("isdroptarget", 159)
        dictCells.Add("issnaptarget", 160)
        dictCells.Add("istextedittarget", 161)
        dictCells.Add("keeptextflat", 162)
        dictCells.Add("label", 163)
        dictCells.Add("langid", 164)
        dictCells.Add("layermember", 165)
        dictCells.Add("leftmargin", 166)
        dictCells.Add("letterspace", 167)
        dictCells.Add("lineadjustfrom", 168)
        dictCells.Add("lineadjustto", 169)
        dictCells.Add("linecap", 170)
        dictCells.Add("linecolor", 171)
        dictCells.Add("linecolortrans", 172)
        dictCells.Add("linegradientangle", 173)
        dictCells.Add("linegradientdir", 174)
        dictCells.Add("linegradientenabled", 175)
        dictCells.Add("linejumpcode", 176)
        dictCells.Add("linejumpfactorx", 177)
        dictCells.Add("linejumpfactory", 178)
        dictCells.Add("linejumpstyle", 179)
        dictCells.Add("linepattern", 180)
        dictCells.Add("linerouteext", 181)
        dictCells.Add("linetolinex", 182)
        dictCells.Add("linetoliney", 183)
        dictCells.Add("linetonodex", 184)
        dictCells.Add("linetonodey", 185)
        dictCells.Add("lineweight", 186)
        dictCells.Add("localizemerge", 187)
        dictCells.Add("lock", 188)
        dictCells.Add("lockaspect", 189)
        dictCells.Add("lockbegin", 190)
        dictCells.Add("lockcalcwh", 191)
        dictCells.Add("lockcrop", 192)
        dictCells.Add("lockcustprop", 193)
        dictCells.Add("lockdelete", 194)
        dictCells.Add("lockend", 195)
        dictCells.Add("lockformat", 196)
        dictCells.Add("lockfromgroupformat", 197)
        dictCells.Add("lockgroup", 198)
        dictCells.Add("lockheight", 199)
        dictCells.Add("lockmovex", 200)
        dictCells.Add("lockmovey", 201)
        dictCells.Add("lockpreview", 202)
        dictCells.Add("lockreplace", 203)
        dictCells.Add("lockrotate", 204)
        dictCells.Add("lockselect", 205)
        dictCells.Add("locktextedit", 206)
        dictCells.Add("lockthemecolors", 207)
        dictCells.Add("lockthemeconnectors", 208)
        dictCells.Add("lockthemeeffects", 209)
        dictCells.Add("lockthemefonts", 210)
        dictCells.Add("lockvtxedit", 211)
        dictCells.Add("lockwidth", 212)
        dictCells.Add("locpinx", 213)
        dictCells.Add("locpiny", 214)
        dictCells.Add("markerindex", 215)
        dictCells.Add("menu", 216)
        dictCells.Add("name", 217)
        dictCells.Add("nameuniv", 218)
        dictCells.Add("newwindow", 219)
        dictCells.Add("noalignbox", 220)
        dictCells.Add("nocoauth", 221)
        dictCells.Add("noctlhandles", 222)
        dictCells.Add("nofill", 223)
        dictCells.Add("noline", 224)
        dictCells.Add("nolivedynamics", 225)
        dictCells.Add("nonprinting", 226)
        dictCells.Add("noobjhandles", 227)
        dictCells.Add("noquickdrag", 228)
        dictCells.Add("noshow", 229)
        dictCells.Add("nosnap", 230)
        dictCells.Add("objectkind", 231)
        dictCells.Add("objtype", 232)
        dictCells.Add("onpage", 233)
        dictCells.Add("outputformat", 234)
        dictCells.Add("overline", 235)
        dictCells.Add("pagebottommargin", 236)
        dictCells.Add("pageheight", 237)
        dictCells.Add("pageleftmargin", 238)
        dictCells.Add("pagelinejumpdirx", 239)
        dictCells.Add("pagelinejumpdiry", 240)
        dictCells.Add("pagelockreplace", 241)
        dictCells.Add("pagerightmargin", 242)
        dictCells.Add("pagescale", 243)
        dictCells.Add("pageshapesplit", 244)
        dictCells.Add("pagesx", 245)
        dictCells.Add("pagesy", 246)
        dictCells.Add("pagetopmargin", 247)
        dictCells.Add("pagewidth", 248)
        dictCells.Add("paperkind", 249)
        dictCells.Add("papersource", 250)
        dictCells.Add("path", 251)
        dictCells.Add("perspective", 252)
        dictCells.Add("pinx", 253)
        dictCells.Add("piny", 254)
        dictCells.Add("placedepth", 255)
        dictCells.Add("placestyle", 257)
        dictCells.Add("plowcode", 258)
        dictCells.Add("pos", 259)
        dictCells.Add("position", 260)
        dictCells.Add("previewquality", 261)
        dictCells.Add("previewscope", 262)
        dictCells.Add("print", 263)
        dictCells.Add("printgrid", 264)
        dictCells.Add("printpageorientation", 265)
        dictCells.Add("prompt", 266)
        dictCells.Add("quickstyleeffectsmatrix", 267)
        dictCells.Add("quickstylefillcolor", 268)
        dictCells.Add("quickstylefillmatrix", 269)
        dictCells.Add("quickstylefontcolor", 270)
        dictCells.Add("quickstylelinecolor", 271)
        dictCells.Add("quickstylelinematrix", 272)
        dictCells.Add("quickstyleshadowcolor", 273)
        dictCells.Add("quickstyletype", 274)
        dictCells.Add("readonly", 275)
        dictCells.Add("reflectionblur", 276)
        dictCells.Add("reflectiondist", 277)
        dictCells.Add("reflectionsize", 278)
        dictCells.Add("reflectiontrans", 279)
        dictCells.Add("relationships", 280)
        dictCells.Add("relchanged", 281)
        dictCells.Add("replacelockformat", 282)
        dictCells.Add("replacelockshapedata", 283)
        dictCells.Add("replacelocktext", 284)
        dictCells.Add("resizemode", 285)
        dictCells.Add("resizepage", 286)
        dictCells.Add("reviewerid", 287)
        dictCells.Add("rightmargin", 288)
        dictCells.Add("rotategradientwithshape", 289)
        dictCells.Add("rotationtype", 290)
        dictCells.Add("rotationxangle", 291)
        dictCells.Add("rotationyangle", 292)
        dictCells.Add("rotationzangle", 293)
        dictCells.Add("rounding", 294)
        dictCells.Add("routestyle", 295)
        dictCells.Add("scalex", 296)
        dictCells.Add("scaley", 297)
        dictCells.Add("selectmode", 298)
        dictCells.Add("shapefixedcode", 299)
        dictCells.Add("shapekeywords", 300)
        dictCells.Add("shapepermeableplace", 301)
        dictCells.Add("shapepermeablex", 302)
        dictCells.Add("shapepermeabley", 303)
        dictCells.Add("shapeplaceflip", 304)
        dictCells.Add("shapeplacestyle", 305)
        dictCells.Add("shapeplowcode", 306)
        dictCells.Add("shaperoutestyle", 307)
        dictCells.Add("shapeshdwblur", 308)
        dictCells.Add("shapesshdwobliqueangle", 309)
        dictCells.Add("shapeshdwoffsetx", 310)
        dictCells.Add("shapeshdwoffsety", 311)
        dictCells.Add("shapeshdwscalefactor", 312)
        dictCells.Add("shapeshdwshow", 313)
        dictCells.Add("shapeshdwtype", 314)
        dictCells.Add("shapesplit", 315)
        dictCells.Add("shapesplittable", 316)
        dictCells.Add("sharpen", 317)
        dictCells.Add("shdwforegnd", 318)
        dictCells.Add("shdwforegndtrans", 319)
        dictCells.Add("shdwobliqueangle", 320)
        dictCells.Add("shdwoffsetx", 321)
        dictCells.Add("shdwoffsety", 322)
        dictCells.Add("shdwpattern", 323)
        dictCells.Add("shdwscalefactor", 324)
        dictCells.Add("shdwtype", 325)
        dictCells.Add("size", 326)
        dictCells.Add("sketchamount", 327)
        dictCells.Add("sketchenabled", 328)
        dictCells.Add("sketchfillchange", 329)
        dictCells.Add("sketchlinechange", 330)
        dictCells.Add("sketchlineweight", 331)
        dictCells.Add("sketchseed", 332)
        dictCells.Add("snap", 333)
        dictCells.Add("softedgesize", 334)
        dictCells.Add("sortkey", 335)
        dictCells.Add("spafter", 336)
        dictCells.Add("spbefore", 337)
        dictCells.Add("spline", 338)
        dictCells.Add("status", 339)
        dictCells.Add("strikethru", 340)
        dictCells.Add("style", 341)
        dictCells.Add("subaddress", 342)
        dictCells.Add("tagname", 343)
        dictCells.Add("textbkgnd", 344)
        dictCells.Add("textbkgndtrans", 345)
        dictCells.Add("textdirection", 346)
        dictCells.Add("textposafterbullet", 347)
        dictCells.Add("thedata", 348)
        dictCells.Add("theme", 349)
        dictCells.Add("themeindex", 350)
        dictCells.Add("thememodern", 351)
        dictCells.Add("thetext", 352)
        dictCells.Add("topmargin", 353)
        dictCells.Add("transparency", 354)
        dictCells.Add("txtangle", 355)
        dictCells.Add("txtheight", 356)
        dictCells.Add("txtlocpinx", 357)
        dictCells.Add("txtlocpiny", 358)
        dictCells.Add("txtpinx", 359)
        dictCells.Add("txtpiny", 360)
        dictCells.Add("txtwidth", 361)
        dictCells.Add("type", 362)
        dictCells.Add("uivisibility", 363)
        dictCells.Add("updatealignbox", 364)
        dictCells.Add("usegroupgradient", 365)
        dictCells.Add("value", 366)
        dictCells.Add("verify", 367)
        dictCells.Add("verticalalign", 368)
        dictCells.Add("viewmarkup", 369)
        dictCells.Add("visible", 370)
        dictCells.Add("walkpreference", 371)
        dictCells.Add("width", 372)
        dictCells.Add("x", 373)
        dictCells.Add("xcon", 374)
        dictCells.Add("xdyn", 375)
        dictCells.Add("xgriddensity", 376)
        dictCells.Add("xgridorigin", 377)
        dictCells.Add("xgridspacing", 378)
        dictCells.Add("xjustify", 379)
        dictCells.Add("xrulerdensity", 380)
        dictCells.Add("xrulerorigin", 381)
        dictCells.Add("y", 382)
        dictCells.Add("ycon", 383)
        dictCells.Add("ydyn", 384)
        dictCells.Add("ygriddensity", 385)
        dictCells.Add("ygridorigin", 386)
        dictCells.Add("ygridspacing", 387)
        dictCells.Add("yjustify", 388)
        dictCells.Add("yrulerdensity", 389)
        dictCells.Add("yrulerorigin", 390)
        dictCells.Add("zorderchanged", 391)

        Return dictCells

    End Function

    '
    '
    '
    Public Structure stcCell

        Private _cellName As String
        Public Property cellName As String
            Get
                cellName = Me._cellName
            End Get
            Set(value As String)
                Me._cellName = value
            End Set
        End Property
        Private _cellValue As String
        Public Property cellValue As String
            Get
                cellValue = Me._cellValue
            End Get
            Set(value As String)
                Me._cellValue = value
            End Set
        End Property
        Private _cellFormula As String
        Public Property cellFormula As String
            Get
                cellFormula = Me._cellFormula
            End Get
            Set(value As String)
                Me._cellFormula = value
            End Set
        End Property
        Private _cellUnits As String
        Public Property cellUnits As String
            Get
                cellUnits = Me._cellUnits
            End Get
            Set(value As String)
                Me._cellUnits = value
            End Set
        End Property
        Private _cellError As String
        Public Property cellError As String
            Get
                cellError = Me._cellError
            End Get
            Set(value As String)
                Me._cellError = value
            End Set
        End Property



        Private _cellNewName As String
        Public ReadOnly Property cellNewName As String
            Get
                cellNewName = _cellNewName
            End Get
        End Property

        Private _cellNewValue As String
        Public ReadOnly Property cellNewValue As String
            Get
                cellNewValue = _cellNewValue
            End Get
        End Property

        '
        '
        '
        Public Sub New _
            (ByVal cName As String, _
             ByVal cValue As String, _
             ByVal cFormula As String, _
             ByVal cUnits As String)

            Me.cellName = cName
            Me.cellValue = cValue
            Me.cellFormula = cFormula
            Me.cellUnits = cUnits
            Me.cellError = Nothing
            Me._cellNewName = Nothing
            Me._cellNewValue = Nothing
        End Sub

        '
        ' reset the fields
        '
        Public Sub Reset()
            Me.cellName = Nothing
            Me.cellValue = Nothing
            Me.cellFormula = Nothing
            Me.cellUnits = Nothing
            Me.cellError = Nothing
            Me._cellNewName = Nothing
            Me._cellNewValue = Nothing
        End Sub

        '
        ' output the cell as a string
        '
        Public Overrides Function toString() As String

            Dim sb As StringBuilder = New StringBuilder
            sb.Append("[")
            If Me.cellName IsNot Nothing Then
                sb.Append("N=""" & Me.cellName & """ ")
            End If
            If Me.cellValue IsNot Nothing Then
                sb.Append("V=""" & Me.cellValue & """ ")
            End If
            If Me.cellFormula IsNot Nothing Then
                sb.Append("F=""" & Me.cellFormula & """ ")
            End If
            If Me.cellUnits IsNot Nothing Then
                sb.Append("U=""" & Me.cellUnits & """ ")
            End If
            If Me.cellError IsNot Nothing Then
                sb.Append("E=""" & Me.cellError & """ ")
            End If
            sb.Append("]")

            Return sb.ToString
            sb = Nothing
            System.GC.Collect()

        End Function

        '
        ' output the cell as xml string 
        '
        Public Function toXML() As String

            Dim sb As StringBuilder = New StringBuilder
            sb.Append("<Cell ")
            If Me.cellName IsNot Nothing Then
                sb.Append("N=""" & Me.cellName & """ ")
            End If
            If Me.cellValue IsNot Nothing Then
                sb.Append("V=""" & Me.cellValue & """ ")
            End If
            If Me.cellFormula IsNot Nothing Then
                sb.Append("F=""" & Me.cellFormula & """ ")
            End If
            If Me.cellUnits IsNot Nothing Then
                sb.Append("U=""" & Me.cellUnits & """ ")
            End If
            sb.Append("/>")

            Return sb.ToString
            sb = Nothing
            System.GC.Collect()

        End Function

        '
        ' we can feed this from just about anything
        '
        Public Sub readXElement _
                (ByVal elemObject As XElement)

            Try

                Me.cellFormula = Nothing
                Me.cellName = Nothing
                Me.cellValue = Nothing
                Me.cellUnits = Nothing
                Me.cellError = Nothing
                Me._cellNewName = Nothing
                Me._cellNewValue = Nothing

                ' first the attributes for the cell
                Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In elemObject.Attributes() _
                    Select att

                For Each att As XAttribute In listOfAttributes
                    'Debug.Print("Att {0}={1}", att.Name.LocalName, att.Value)
                    Select Case LCase(att.Name.LocalName)
                        Case "n"
                            Me.cellName = att.Value
                        Case "v"
                            Me.cellValue = CStr(att.Value)
                        Case "f"
                            Me.cellFormula = att.Value
                        Case "u"
                            Me.cellUnits = att.Value
                        Case "e"
                            Me.cellError = att.Value
                    End Select
                Next att

                ' build a new value for when we want to put this into a list
                '
                Me._cellNewName = "cell" & "_" & Me.cellName
                ' give priority to formulas over values
                If (Me.cellFormula IsNot Nothing) Then
                    If (LCase(Me.cellFormula) <> "noformula") _
                        And (LCase(Me.cellFormula) <> "inh") Then
                        _cellNewValue = "cellF" & " = " & Me.cellFormula
                        Exit Sub
                    End If ' test  for noformula
                End If ' test for not nothing
                If Me.cellValue IsNot Nothing Then
                    _cellNewValue = "cellV" & " = " & Me.cellValue
                    Exit Sub
                End If ' test for value



            Catch errXml As XmlException
                Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
                If (rethrow) Then
                    Throw
                End If
            Catch err As Exception
                Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
                If (rethrow) Then
                    Throw
                End If
            End Try

        End Sub

        '
        '
        '
        Public Function writeXelement() As XElement

            Dim xelemReturn As XElement = Nothing

            xelemReturn = XElement.Load((XmlReader.Create(New StringReader(toXML))))

            Return xelemReturn

        End Function

        '
        '
        '
        Public Shared Operator =(ByVal stcA As stcCell, ByVal stcB As stcCell) As Boolean
            If stcA.Equals(stcB) Then
                Return True
            Else
                Return False
            End If
        End Operator

        '
        '
        '
        Public Shared Operator <>(ByVal stcA As stcCell, ByVal stcB As stcCell) As Boolean
            If Not stcA.Equals(stcB) Then
                Return True
            Else
                Return False
            End If
        End Operator

    End Structure

    '
    '
    '
    Public Structure stcCellSection

        ' we can theoretically merge the dictionaries so we should keep track of 
        ' where we found the definition
        Private _csSource As String
        Public Property csSource As String
            Get
                csSource = Me._csSource
            End Get
            Set(value As String)
                Me._csSource = value
            End Set
        End Property

        Private _csCellName As String
        Public Property csCellName As String
            Get
                csCellName = Me._csCellName
            End Get
            Set(value As String)
                Me._csCellName = value
            End Set
        End Property

        ' in xml this really isn't the parent, since that might be a "row"
        ' this looks past the row to the name of the "section" that it was
        ' found in (if not standalone). we put *** as a null 
        Private _csCellParent As String
        Public Property csCellParent As String
            Get
                csCellParent = Me._csCellParent
            End Get
            Set(value As String)
                Me._csCellParent = value
            End Set
        End Property

    End Structure


#End Region



    '
    ' working with binary flags in object type
    '
    'Dim objFlag As stcShapeObjType = New stcShapeObjType
    'objFlag.ulngMask = CLng(strValue)
    'Dim bln1D As Boolean = objFlag.IsEqual(stcShapeObjType.Flag.visLOFlagsRoutable)

    Public Structure stcFlagObjType


        '&H0     Default. The application decides based on the drawing context.      visLOFlagsVisDecides
        '&H1     Shape is placeable.                                                 visLOFlagsPlacable
        '&H2     Shape is routable. Must be a one-dimensional (1-D) shape.           visLOFlagsRoutable
        '&H4     Shape is not placeable, not routable.                               visLOFlagsDont
        '&H8     Group contains placeable/routable shapes.                           visLOFlagsPNRGroup

        <FlagsAttribute()>
        Public Enum Flag As ULong
            visLOFlagsVisDecides = &H0
            visLOFlagsPlacable = &H1
            visLOFlagsRoutable = &H2
            visLOFlagsDont = &H4
            visLOFlagsPNRGroup = &H8
        End Enum

        '
        '
        '
        Private _ulngMask As ULong
        Public Property ulngMask() As ULong
            Get
                Return _ulngMask
            End Get
            Set(value As ULong)
                _ulngMask = value
            End Set
        End Property

        '
        '
        '
        Private Sub SetField(lclFlag As Flag)
            ulngMask = CULng(lclFlag)
        End Sub

        '
        '
        '
        Public Sub SetOn(lclFlag As Flag)
            ulngMask = ulngMask Or CULng(lclFlag)
        End Sub

        '
        '
        '
        Public Sub SetOff(lclFlag As Flag)
            ulngMask = ulngMask And Not CULng(lclFlag)
        End Sub

        '
        '
        '
        Public Sub SetToggle(lclFlag As Flag)
            ulngMask = ulngMask Xor CULng(lclFlag)
        End Sub


        '
        '
        '
        Public Function AnyOn(lclFlag As Flag) As Boolean
            Return (ulngMask And CULng(lclFlag)) <> 0
        End Function

        '
        '
        '
        Public Function AllOn(lclFlag As Flag) As Boolean
            Return (ulngMask And CULng(lclFlag)) = CULng(lclFlag)
        End Function

        '
        '
        '
        Public Function IsEqual(lclFlag As Flag) As Boolean
            Return ulngMask = CULng(lclFlag)
        End Function

        '
        '
        '
        Public Shared Function DecimalToFlag(dec As Decimal) As Flag
            Dim lclFlag As Flag = Flag.visLOFlagsVisDecides ' &H0
            Dim tMsk As ULong = 0
            Dim shift As Byte
            Try
                shift = CByte(Math.Truncate(dec))
                If shift > 0 AndAlso shift <= 64 Then
                    tMsk = CULng(&H1) << (shift - 1)
                End If
                lclFlag = DirectCast(tMsk, Flag)
            Catch e As OverflowException
                'Byte cast operation
                Console.WriteLine("Exception caught in ToStringBin: {0}", e.ToString())
            End Try

            Return lclFlag
        End Function

    End Structure

    '
    ' dictionary is kept in clsMasters
    '
    Public Structure stcMasterData
        Public mstId As Long
        Public mstName As String
        Public mstNameU As String
        Public mstResource As String
        Public mstFile As String
        Public mstShpObjType As stcFlagObjType
    End Structure

    '
    '
    '
    Public Structure stcPageData
        Public pgId As Long
        Public pgName As String
        Public pgNameU As String
        Public pgResource As String
        Public pgFile As String

        Public Sub New(ByVal newPgId As Long, _
                       ByVal newPgName As String, _
                       ByVal newPgNameU As String, _
                       ByVal newPgResrce As String, _
                       ByVal newPgFile As String)

            pgId = newPgId
            pgName = newPgName
            pgNameU = newPgNameU
            pgResource = newPgResrce
            pgFile = newPgFile

        End Sub
    End Structure



End Class
